package com.chii.colorization.ui.activity;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.chii.colorization.R;
import com.chii.colorization.utils.SPUtil;
import com.chii.colorization.utils.connes.OkHttpUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.chii.colorization.utils.ActivityCodes.REQUESTCODE_FROM_PAY;

public class UserInfoActivity extends AppCompatActivity {
    public static String TAG = UserInfoActivity.class.getName();

    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.logout)
    Button logout;
    @BindView(R.id.vip)
    TextView TV_vip;
    @BindView(R.id.order)
    TextView TV_order;
    private String saveUsername;
    private Boolean vip;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        initHandler();
        //初始化返回按键
        initAction();
        //初始化用户信息
        initData();
        initView();

    }


    private void initData() {
        saveUsername = (String) SPUtil.get(UserInfoActivity.this, "username", "");
        vip = (Boolean) SPUtil.get(UserInfoActivity.this, "vip", Boolean.FALSE);

    }

    private void initView() {
        username.setText(saveUsername);
        if (vip) {
            TV_vip.setText("你已经是VIP啦!!");
            TV_vip.setEnabled(false);
            //显示订单按键
            TV_order.setVisibility(View.VISIBLE);
        }


    }

    private void initAction() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.username, R.id.vip, R.id.logout,R.id.order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.username:
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("username", username.getText());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                Toast.makeText(this, "用户名已粘贴到剪贴板", Toast.LENGTH_SHORT).show();
                break;
            case R.id.vip:
                Intent intent = new Intent();
                intent.setClass(UserInfoActivity.this, PayActivity.class);
                intent.putExtra("data", "post");
                startActivityForResult(intent, REQUESTCODE_FROM_PAY);
                break;
            case R.id.order:
                //打开订单
                Intent intent2 = new Intent();
                intent2.setClass(UserInfoActivity.this, PayActivity.class);
                intent2.putExtra("data","get");
                startActivity(intent2);
                break;
            case R.id.logout:
                //删除登录cookie
                OkHttpUtil.delCookie(this);
                SPUtil.remove(UserInfoActivity.this, "token");
                Intent intent3 = new Intent();
                setResult(Activity.RESULT_OK, intent3);
                finish();
                break;
            default:
        }
    }


    private void initHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 500:

                        Toast.makeText(UserInfoActivity.this, "无法使用！！", Toast.LENGTH_SHORT).show();

                    default:
                }
                return false;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 当otherActivity中返回数据的时候，会响应此方法
        // requestCode和resultCode必须与请求startActivityForResult()和返回setResult()的时候传入的值一致。
        Log.d(TAG, "onActivityResult: " + requestCode + "  " + resultCode);
        Log.d(TAG, "RESULT_OK " + Activity.RESULT_OK);
        switch (requestCode) {
            case REQUESTCODE_FROM_PAY:
                if (resultCode == Activity.RESULT_OK) {
                    Log.d(TAG, "RegisterActivity.REQUESTCODE_FROM_REGISTER ");
                    Toast.makeText(UserInfoActivity.this, "恭喜你成为VIP！！", Toast.LENGTH_SHORT).show();

                    SPUtil.put(UserInfoActivity.this, "vip", true);
                    initData();
                    initView();
                }
                break;
            default:
                break;
        }

    }


}
